package com.ruoyi.standingbook.cleaning.controller;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;

import javax.servlet.http.HttpServletResponse;
import javax.validation.constraints.NotEmpty;
import javax.validation.constraints.NotNull;

import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.annotation.RepeatSubmit;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.PageQuery;
import com.ruoyi.common.core.domain.R;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.validate.AddGroup;
import com.ruoyi.common.core.validate.EditGroup;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.standingbook.cleaning.domain.bo.CleaningImportBaseBo;
import com.ruoyi.standingbook.cleaning.domain.vo.CleaningImportBaseVo;
import com.ruoyi.standingbook.cleaning.domain.vo.CleaningImportDetailVo;
import com.ruoyi.standingbook.cleaning.service.ICleaningImportBaseService;
import com.ruoyi.standingbook.cleaning.service.impl.CleaningExcelService;

import cn.dev33.satoken.annotation.SaCheckPermission;
import lombok.RequiredArgsConstructor;

/**
 * 清扫保洁导入
 *
 * @author cc
 * @date 2023-04-27
 */
@Validated
@RequiredArgsConstructor
@RestController
@RequestMapping("/standingbook/cleaningImportBase")
public class CleaningImportBaseController extends BaseController {

    private final ICleaningImportBaseService iCleaningImportBaseService;
    private final CleaningExcelService cleaningExcelService;
    /**
     * 查询清扫保洁导入列表
     */
    @SaCheckPermission("standingbook:cleaningImportBase:list")
    @GetMapping("/list")
    public TableDataInfo<CleaningImportBaseVo> list(CleaningImportBaseBo bo, PageQuery pageQuery) {
        return iCleaningImportBaseService.queryPageList(bo, pageQuery);
    }

    /**
     * 导出清扫保洁导入列表
     */
    @SaCheckPermission("standingbook:cleaningImportBase:export")
    @Log(title = "清扫保洁导入", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(CleaningImportBaseBo bo, HttpServletResponse response) {
        List<CleaningImportBaseVo> list = iCleaningImportBaseService.queryList(bo);
        ExcelUtil.exportExcel(list, "清扫保洁导入", CleaningImportBaseVo.class, response);
    }

    /**
     * 获取清扫保洁导入详细信息
     *
     * @param batchNo 主键
     */
    @SaCheckPermission("standingbook:cleaningImportBase:query")
    @GetMapping("/{batchNo}")
    public R<CleaningImportBaseVo> getInfo(@NotNull(message = "主键不能为空")
                                     @PathVariable String batchNo) {
        return R.ok(iCleaningImportBaseService.queryById(batchNo));
    }

    /**
     * 新增清扫保洁导入
     */
    @SaCheckPermission("standingbook:cleaningImportBase:add")
    @Log(title = "清扫保洁导入", businessType = BusinessType.INSERT)
    @RepeatSubmit()
    @PostMapping()
    public R<Void> add(@Validated(AddGroup.class) @RequestBody CleaningImportBaseBo bo) {
        return toAjax(iCleaningImportBaseService.insertByBo(bo));
    }

    /**
     * 修改清扫保洁导入
     */
    @SaCheckPermission("standingbook:cleaningImportBase:edit")
    @Log(title = "清扫保洁导入", businessType = BusinessType.UPDATE)
    @RepeatSubmit()
    @PutMapping()
    public R<Void> edit(@Validated(EditGroup.class) @RequestBody CleaningImportBaseBo bo) {
        return toAjax(iCleaningImportBaseService.updateByBo(bo));
    }

    /**
     * 删除清扫保洁导入
     *
     * @param batchNos 主键串
     */
    @SaCheckPermission("standingbook:cleaningImportBase:remove")
    @Log(title = "清扫保洁导入", businessType = BusinessType.DELETE)
    @DeleteMapping("/{batchNos}")
    public R<Void> remove(@NotEmpty(message = "主键不能为空")
                          @PathVariable String[] batchNos) {
        return toAjax(iCleaningImportBaseService.deleteWithValidByIds(Arrays.asList(batchNos), true));
    }
    
    /**
     * 新增导入主
     */
    @SaCheckPermission("standingbook:cleaningImportBase:add")
    @Log(title = "清扫保洁导入", businessType = BusinessType.IMPORT)
    @RepeatSubmit()
    @PostMapping("/importData")
    public R<Void> importData(@Validated(AddGroup.class) @RequestBody CleaningImportBaseBo bo) {
        return toAjax(this.cleaningExcelService.importExcel(bo));
    }
    
    /**
     * 获取导入模板
     */
    @PostMapping("/importTemplate")
    public void importTemplate(HttpServletResponse response) {
        ExcelUtil.exportExcel(new ArrayList<>(), "清扫保洁数据", CleaningImportDetailVo.class, response);
    }
}
